/**
 * Copyright (C) 2013 The yydorid-framework project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.yangying.androidframework.core.task;

import net.yangying.androidframework.core.model.YYDroid;
import android.os.AsyncTask;
import android.util.Log;

/**
 * @author yangying
 * @date 2013-1-14 下午4:28:36
 * @version 1.0
 *
 */
public abstract class AbstractAsyncTask extends AsyncTask<Object, Integer, YYDroid> {
	private static final String TAG = AbstractAsyncTask.class.getSimpleName();

//        private AsyncTaskHandler handler;
        private Exception mReason;

//        public AbstractAsyncTask(AsyncTaskHandler handler) {
//            this.handler = handler;
//        }

        @Override
        protected void onPreExecute() {
//        	handler.onTaskPreExecute();
        	onTaskPreExecute();
        }

        @Override
        protected YYDroid doInBackground(Object... params) {
            try {
                return onTaskDoInBackground(params);
            } catch (Exception e) {
                mReason = e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(YYDroid yyDroid) {
//            if (handler != null) {
//            	handler.onTaskCompleted(yyDroid, mReason);
//            }
        	onTaskCompleted(yyDroid,mReason);
        }

        @Override  
        protected void onProgressUpdate(Integer... progresses) {  
            Log.i(TAG, "onProgressUpdate(Progress... progresses) called");  
//            progressBar.setProgress(progresses[0]);  
//            textView.setText("loading..." + progresses[0] + "%");
            onTaskProgressUpdate(progresses);
        }
        
        @Override
        protected void onCancelled() {
//            if (handler != null) {
//            	handler.onTaskCompleted(null, mReason);
//            }
        	onTaskCancelled();
        }
    	protected abstract void onTaskPreExecute();
    	protected abstract YYDroid onTaskDoInBackground(Object... params);
    	protected abstract void onTaskCompleted(YYDroid yyDroid, Exception exception);
    	protected abstract void onTaskProgressUpdate(Integer... progresses);
    	protected abstract void onTaskCancelled();
   
}
